//
//  main.cpp
//  kerberos_test
//
//  Created by Constantine Mars on 5/31/13.
//  Copyright (c) 2013 Constantine Mars. All rights reserved.
//

#include <iostream>
#include <err.h>
#include <krb5.h>
//#include <netdb.h>
//#include <sys/socket.h>
//#include <sys/types.h>
//#include <unistd.h>
//#include <netinet/in.h>
//#include <arpa/inet.h>

#include<sys/socket.h>
#include<netinet/in.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <arpa/inet.h>
//#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

//http://www.h5l.org/manual/HEAD/krb5/krb5_introduction.html

int main(int argc, const char * argv[])
{

    krb5_context context;
    
    if (krb5_init_context(&context))
        errx (1, "krb5_context");
    
    
    //TCP CLIENT
    char buf[100];
    struct sockaddr_in client;
    int sock_desc,k;
    sock_desc = socket(AF_INET,SOCK_STREAM,0);
    memset(&client,0,sizeof(client));
    client.sin_family = AF_INET;
    client.sin_addr.s_addr = inet_addr("127.0.0.1");
    client.sin_port = htons(7777);
    k = connect(sock_desc,(struct sockaddr*)&client,sizeof(client));
    if (k == -1)
    {
        printf("connect error: %d\n", k);
    }
    
    while(1)
    {
        gets(buf);
        k = send(sock_desc,buf,100,0);
        if (k > 0)
        {
        if(strcmp(buf,"exit")==0)
            break;
        }
        else
        {
            printf("recv error: %d\n", errno);
        }
    }
    close(sock_desc);
    
    krb5_auth_context auth_context;
    
    krb5_error_code status = krb5_auth_con_init (context, &auth_context);
    if (status)
        krb5_err (context, 1, status, "krb5_auth_con_init");
    
    
    std::cout << "kerberos test finished\n";
    return 0;
}

